package com.ccys.app.init;

import com.ccys.common.constants.QSwaggerContext;
import com.ccys.common.constants.TouristUtil;
import com.ccys.common.enums.CodeEnum;
import com.ccys.common.util.JedisUtil;
import com.ccys.common.util.SerializationUtil;
import com.ccys.db.cache.SysCodeUtil;
import com.ccys.db.entity.SysCode;
import com.ccys.db.mapper.SysCodeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Configuration
@Slf4j
public class AppInit implements CommandLineRunner {

    @Resource
    private SysCodeMapper codeMapper;

    @Override
    public void run(String... args) throws Exception {

        //加载游客模式
        initTouristApi();

        //swagger上下文加载
        setContext();

        //初始化系统参数
        initCode();


    }

    /**
     * 游客模式
     */
    private void initTouristApi(){
        Map tourist = TouristUtil.tourist();
        Set set = tourist.keySet();
        for (Object key:set
        ) {
            log.info(String.valueOf(key));
            log.info(String.valueOf(tourist.get(key)));
        }
    }

    /**
     * 加载swagger上下文
     */
    private void setContext() throws ClassNotFoundException {
        log.info("============正在加载必要上下文============");
        QSwaggerContext.setContext();
        Map<String, List<String>> contentMap = QSwaggerContext.CONTENT_MAP;
        for (String key :
                contentMap.keySet()) {
            log.info(key);
            log.info(contentMap.get(key).toString());
        }
    }

    /**
     * 初始化系统参数
     */
    private void initCode() {
        Example example = new Example(SysCode.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", "0");
        List<SysCode> sysCodes = codeMapper.selectByExample(example);
        sysCodes.forEach(c -> {
            SysCodeUtil.updateCode(SysCodeUtil.INSERT,c);
        });
        log.info("===========初始化系统参数缓存完成,总数:"+sysCodes.size()+"=========");
    }

}
